public class Printer {
    public int[] clockwisePrint(int[][] mat, int n, int m) {
    if(mat.length==0){
        return new int[0];
    }
    int left=0;
    int right=m-1;
    int up=0;
    int down=n-1;
    int [] arr=new int[n*m];
    int count=0;
    while(left<=right&&up<=down){
         for(int i=left;i<=right;i++){
          arr[count]=mat[up][i];
             count++;   
    }
        up++;
        if(up>down){
            break;
        }
        for(int j=up;j<=down;j++){
            arr[count]=mat[j][right];
            count++;
        }
        right--;
        if(left>right){
            break;
        }
        for(int q=right;q>=left;q--){
            arr[count]=mat[down][q];
            count++;
        }
        down--;
        if(up>down){
            break;
        }
        for(int p=down;p>=up;p--){
            arr[count]=mat[p][left];
            count++;
        }
        left++;
        if(left>right){
           break;
            }
    }
    return arr;
    }
}